package com.david.controller;

import com.david.lb.ILoadBalancer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;
import java.util.List;
import java.util.UUID;

/**
 * @Description: OrderController
 * @Author: wangwei
 * @CreateDate: 2022/1/13 11:04
 */
@RestController
@Slf4j
public class OrderController {


    @Autowired
    private RestTemplate restTemplate;

    private static final String  url="http://cloud-consul-payment";

    @Resource
    private ILoadBalancer loadBalancer;

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/consumer/payment/consul")
    public String paymentConsul(){

        List<ServiceInstance> serviceInstanceList=discoveryClient.getInstances("cloud-consul-payment");
        for (ServiceInstance si:serviceInstanceList) {
            log.info("ServiceInstance>>{}>>uri>>{}",si,si.getUri());
            
        }

        URI uri=loadBalancer.instances(serviceInstanceList).getUri();

//        return restTemplate.getForObject(url+"/payment/consul",String.class);
        return restTemplate.getForObject(uri+"/payment/consul",String.class);
    }

}

